﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAKS.IServices;
using SAKS.Models;
using NHibernate;
using SAKS.Services.Query;

namespace SAKS.Services
{
    public class AwardServices : ServicesBase<Award>,IAwardServices
    {
        public override void Update(Award award)
        {
            var ori = Session.Get<Award>(award.Id);
            if (ori != null)
            {
                ori.Name = award.Name;
                ori.AwardType = award.AwardType;
                ori.AwardLevel = award.AwardLevel;
                ori.Year = award.Year;
            }
            Session.Update(ori);
        }

        public override object[] List(int first, int pageSize, out int? totalRecords, string key)
        {
            var query = new PagedAwardsSearch(Session) { First = first,PageSize = pageSize,Key = key };
            var result = query.Execute();
            totalRecords = result.TotalItems;
            var ret = from a in result.PageOfResults
                      select new string[]{
                          a.Id.ToString(),
                          a.Name,
                          a.Year.Year + "-" + a.Year.Month,
                          a.AwardType,
                          a.AwardLevel
                      };
            return ret.ToArray();
        }

        public bool IsNameUsed(string name)
        {
            return 0 < Session.QueryOver<Award>().Where(a => a.Name == name).ToRowCountQuery().FutureValue<int>().Value;
        }
    }
}
